package com.example.truthordare;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter extends SQLiteOpenHelper {
	Context context;

	public DBAdapter(Context context) {
		super(context, dbName, null, 33);
		this.context = context;
	}

	static final String dbName = "Database_TruthOrDare";// database name

	// table player
	static final String tbName = "PlayerName";// table name
	// Column
	static final String colNid = "_id";
	static final String colname = "NamePlayer";
	static final String colSex = "Gender";
	static final String colcontact = "Contact";

	// table truth
	static final String tbtruth = "tbQuestion";// table name
	// column
	static final String colTid = "_id";
	static final String colQS = "Question";
	static final String coltype = "Type";
	static final String collv = "LV";

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		Log.i("da", "db name" + dbName);
		try {
			db.execSQL("CREATE TABLE " + tbName + " ( " + colNid
					+ " INTEGER PRIMARY KEY AUTOINCREMENT , " + colname
					+ " TEXT , " + colSex + " INTEGER , " + colcontact
					+ " TEXT )");

			db.execSQL("CREATE TABLE " + tbtruth + " ( " + colTid
					+ " INTEGER PRIMARY KEY AUTOINCREMENT , " + colQS
					+ " TEXT , " + coltype + " INTEGER , " + collv
					+ " INTEGER ) ");
			Log.i(null, "db ok");

		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	public Cursor selectquestion(int type, int lv) {
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cu = db.rawQuery("SELECT * FROM tbQuestion  where Type = "
				+ type + " and LV >= " + lv + " ORDER BY RANDOM() LIMIT 1 ",
				null);
		return cu;
	}

	public void insertPlayer(String name, int gender, String contact) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put(colname, name);
		cv.put(colSex, gender);
		cv.put(colcontact, contact);
		db.insert(tbName, colname, cv);
		Log.i("insert", "insert ok");
		db.close();
	}

	public void updateplayrename(int id, String name) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.execSQL("update PlayerName set NamePlayer = '" + name
				+ "' where _id= ' " + id + "' ");
	}

	public void detele(int id) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.execSQL("delete from PlayerName Where _id ='" + id + "'");

	}

	public void insertquestion() {
		try {
			SQLiteDatabase db = this.getWritableDatabase();
			db.execSQL("insert into "
					+ tbtruth
					+ " ("
					+ colQS
					+ " , "
					+ coltype
					+ " , "
					+ collv
					+ ") values('If you only had 24 hours left to live, what would you do?','1','2');");

			db.execSQL("insert into "
					+ tbtruth
					+ " ("
					+ colQS
					+ " , "
					+ coltype
					+ " , "
					+ collv
					+ ") values('Go outside and sing your favorite nursery rhyme.','2','1');");

			db.execSQL("insert into "
					+ tbtruth
					+ " ("
					+ colQS
					+ " , "
					+ coltype
					+ " , "
					+ collv
					+ ") values('What is the best thing you have ever gotten away with?','1','1');");
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	Cursor getAllPlayer() {
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cur = db.rawQuery("SELECT * FROM " + tbName, null);
		Log.i("", "cur " + cur.getCount());
		return cur;
	}

	Cursor chekquestion() {
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cur = db.rawQuery("SELECT * FROM " + tbtruth, null);
		Log.i("", "cur " + cur.getCount());
		return cur;
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		db.execSQL("DROP TABLE IF EXISTS " + tbName);
		db.execSQL("DROP TABLE IF EXISTS " + tbtruth);
		onCreate(db);

	}

}
